package com.example.demo.thread;

/**
 * 项目名称：hello-6
 * 类 名 称：TestThread
 * 类 描 述：
 * 创建时间：2021/4/24 00:06
 * 创 建 人：wangsheng
 */
public class TestThread implements Runnable {
    private int num = 20;
    private boolean flag = true;


    @Override
    public void run() {
        while (flag) {
            if (num <= 0) {
                flag = false;
                break;
            }
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (this) {
                System.out.println(Thread.currentThread().getName() + "拿到第" + num-- + "张票！");
            }
        }
    }

    public static void main(String[] args) {
        TestThread testThread = new TestThread();
        new Thread(testThread, "张三").start();
        new Thread(testThread, "李四").start();
        new Thread(testThread, "黄牛党").start();
    }
}
